package com.example.demo.controller;

import com.example.demo.entity.Response;
import com.example.demo.entity.Student;
import com.example.demo.entity.StudentAccount;
import com.example.demo.service.Impl.StudentImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/student")
@RequiredArgsConstructor
public class StudentController {
    private final StudentImpl studentImpl;

    @GetMapping("/getAllStudent")
    public ResponseEntity<Response> getAllStudent() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getAllStudent",studentImpl.getAllStudent()))
                        .message("获取所有学员信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @PostMapping("/addStudent")
    public ResponseEntity<Response> addStudent(@RequestBody Student student){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addStudent",studentImpl.addStudent(student)))
                            .message("新增学员")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("新增失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @PostMapping("/addStudentandAccount")
    public ResponseEntity<Response> addStudent(@RequestBody Student student,@RequestParam Long studentAccountId){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addStudent",studentImpl.addStudent(student,studentAccountId)))
                            .message("新增学员")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("新增失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @DeleteMapping("/deleteStudent/{studentId}")
    public ResponseEntity<Response> deleteStudent(@PathVariable Long studentId) {
        try{
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteCurrent", studentImpl.deleteStudent(studentId)))
                            .message("删除成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @PostMapping("/updateStudent")
    public ResponseEntity<Response> updateStudent(@RequestBody Student student) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateCurrent",studentImpl.updateStudent(student)))
                            .message("更新学员")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新学员失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @PostMapping("/filterStudent")
    public ResponseEntity<Response> filterStudent(@RequestBody Student student) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("filterStudent",studentImpl.filterStudent(student)))
                        .message("查找学员")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @GetMapping("/getStudentByStudentId/{studentId}")
    public ResponseEntity<Response> getStudentByStudentId(@PathVariable("studentId") Long studentId)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getStudentByStudentId",studentImpl.getStudentByStudentId(studentId)))
                        .message("学员ID查找")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/getStudentAccountByStudentId/{studentId}")
    public ResponseEntity<Response> getStudentAccountByStudentId(@PathVariable("studentId") Long studentId)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getStudentAccountByStudentId",studentImpl.getStudentAccountByStudentId(studentId)))
                        .message("学员ID查找")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @PostMapping("/getSelfAccount")
    public ResponseEntity<Response> getSelfAccount(@RequestBody StudentAccount studentAccount)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getSelfAccount",studentImpl.updateSelfAccount(studentAccount)))
                        .message("学员账户更新")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/pageFindStudent")
    public ResponseEntity<Response> pageFindStudent(@RequestParam int page,@RequestParam int size)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("pageFindStudent",studentImpl.pageFindStudent(page,size)))
                        .message("分页查学员")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/updatePassword")
    public ResponseEntity<Response> updatePassword(@RequestParam String account,
                                                   @RequestParam String oldPassword,
                                                   @RequestParam String newPassword) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updatePassword",studentImpl.updatePassword(account,oldPassword,newPassword)))
                            .message("更新密码")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新密码失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
}
